﻿///***************************************************************
/// 시  스  템 : Updms.Mst
/// 단위시스템 : MST
/// 계      층 : Presentation Layer
/// 프로그램명 : Mst1010m
/// 파  일  명 : Mst1010m.aspx.cs
/// 설      명 : 휴일관리
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-10-30 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;
using Updms.Mst.Exe;


namespace Updms.Mst
{
	public partial class Mst1010m : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Mst1010m";
		public static readonly string gs_path_info = "/updms/Mst/" + PID + ".aspx";

		Global global = new Global();

		//검색
		protected string kw1 = "";	//기준년도

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetBaseYear();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{

		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";
			
			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_base_year.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_base_year.SelectedValue = kw1;
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_base_year.SelectedValue;
					break;
			}
		}

		#endregion

		#region 마스터 Method (SetBaseYear, SetPrevNextYear, SetThisYear, SetEnabledYearBtn)

		// 기준년도 설정
		private void SetBaseYear()
		{
			ddl_search_base_year.Items.Clear();

			// 2000년부터 2020년까지
			for (int i = 2000; i <= 2100; i++)
			{
				string ls_year = i.ToString();
				ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
			}
			
			ddl_search_base_year.SelectedValue = DateUtil.GetNow("yyyy");
		}

		//전년, 차년버튼 누르면
		private void SetPrevNextYear(int pi_year_cnt)
		{
			string ls_year = ddl_search_base_year.SelectedValue;
			string ls_calc_year = Convert.ToString(Convert.ToInt32(ls_year) + pi_year_cnt);

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_year))
			{
				ddl_search_base_year.SelectedValue = ls_calc_year;
			}

			//버튼처리
			SetEnabledYearBtn();
		}

		//당년설정
		private void SetThisYear()
		{
			string ls_this_year = DateUtil.GetNow("yyyy");
			ddl_search_base_year.SelectedValue = ls_this_year;
			SetEnabledYearBtn();
		}

		//전년, 차년버튼 처리
		private void SetEnabledYearBtn()
		{
			ibtn_prev_year.Enabled = (ddl_search_base_year.SelectedIndex == 0) ? false : true;
			ibtn_next_year.Enabled = (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1) ? false : true;
		}

		#endregion

		#region CRUD (SelectProcess, SetHolidayYn, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_base_year", typeof(string), kw1);

			using (Mst1010m_NT _Mst1010m_NT = new Mst1010m_NT())
			{
				lo_ds.Merge(_Mst1010m_NT.SelectProcess(lo_dp));
			}

			if (!DataSetUtil.CheckRowCount(lo_ds, "UPDMS_MST_DATE"))
			{
				CallMsgBoxInAjax("MSG_ERR_111");	// 등록된 내용이 없습니다
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();
		}	

		//수정
		private void SetHolidayYn(DataPack lo_dp)
		{
			using (Mst1010m_RT _Mst1010m_RT = new Mst1010m_RT())
			{
				int li_return = _Mst1010m_RT.SetHolidayYn(lo_dp);
				string ls_msg_id = (li_return > 0) ? "MSG_INF_006" : "MSG_ERR_104";	//수정되었습니다:수정할 수 없습니다
				CallMsgBoxInAjax(ls_msg_id);
			}
		}		

		//검색
		private void SearchProcess()
		{
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();
			SelectProcess();
		}

		#endregion

		#region Page_PreRender

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		
		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			//헤더의 rowspan되는 Cell을 삭제함
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Visible = false;
			}
			
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
			    e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
			}

			//멀티헤더설정
			SortedList lo_sl = new SortedList();
			SMGridView sGv = new SMGridView();

			lo_sl.Add("1", GetXmlMsg("STR_015") + ",1,2");
			lo_sl.Add("2", GetXmlMsg("STR_011") + ",3,1");
			lo_sl.Add("3", GetXmlMsg("STR_012") + ",3,1");
			lo_sl.Add("4", GetXmlMsg("STR_013") + ",3,1");
			lo_sl.Add("5", GetXmlMsg("STR_014") + ",3,1");
			sGv.SetMultiHeader(e, lo_sl, "multi_header");
		}		

		#endregion

		#region 버튼이벤트

		//전년
		protected void ibtn_prev_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(-1);
			SearchProcess();
		}

		//당년
		protected void ibtn_this_year_Click(object sender, EventArgs e)
		{
			SetThisYear();
			SearchProcess();
		}

		//차년
		protected void ibtn_next_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(1);
			SearchProcess();
		}

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledYearBtn();
			SearchProcess();
		}

		//저장
		protected void lbtn_set_holiday_yn_Click(object sender, EventArgs e)
		{
			DataPack lo_dp = new DataPack();
			lo_dp.AddProperty("ls_dt", typeof(string), h_dt.Value);

			using (AccessCheck access = new AccessCheck())
			{
				if (access.CheckPgmUsingGrant(USER_DIV, PID))
				{
					SetHolidayYn(lo_dp);
				}
				else
				{
					CallMsgBoxInAjax("MSG_ERR_109");	//작업에 필요한 권한이 없습니다
				}
			}
			SearchProcess();
		}

		#endregion

	}
}
